﻿using System;
using BSPortal.Usercontrol;

namespace BSPortal
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadPage();
        }

        protected void LoadPage()
        {
            short line = Common.GetLine();
            string page = Common.GetPage();

            string title = Common.GetValueInAppSetting("Title" + line, "72H.vn - Mua sắm thật đơn giản");
            this.Title = title;

            #region LoadSpecify

            if (page==PageConst.Cart)
            {
                var control = (Cart)LoadControl("Usercontrol/Cart.ascx");
                MainPlaceHolder.Controls.Add(control);
                return;
            }

            if (page == "ChangeInfo")
            {
                var control = (ChangeInfo)LoadControl("Usercontrol/ChangeInfo.ascx");
                MainPlaceHolder.Controls.Add(control);
                return;
            }

            if (page == PageConst.Login)
            {
                var control = (Login)LoadControl("Usercontrol/Login.ascx");
                MainPlaceHolder.Controls.Add(control);
                return;
            }

            if (page == PageConst.Registry)
            {
                var control = (Registry)LoadControl("Usercontrol/Registry.ascx");
                MainPlaceHolder.Controls.Add(control);
                return;
            }

            if (page == PageConst.Contact)
            {
                var control = (Contact)LoadControl("Usercontrol/Contact.ascx");
                MainPlaceHolder.Controls.Add(control);

                var control2 = (HtmlContent)LoadControl("Usercontrol/HtmlContent.ascx");
                control2.HtmlContentId = short.Parse(Common.GetValueInAppSetting("Contact", "2"));
                MainPlaceHolder.Controls.Add(control2);
                return;
            }

            var control8 = (HtmlContent)LoadControl("Usercontrol/HtmlContent.ascx");
            control8.HtmlContentId = short.Parse(Common.GetValueInAppSetting("Slide" + line, "2"));
            MainPlaceHolder.Controls.Add(control8);

            if (page == PageConst.Search)
            {
                var control = (Search)LoadControl("Usercontrol/Search.ascx");
                control.PageSize = 20;
                MainPlaceHolder.Controls.Add(control);
                return;
            }

            if (page == PageConst.Instruction)
            {
                var control21 = (HtmlContent)LoadControl("Usercontrol/HtmlContent.ascx");
                control21.HtmlContentId = short.Parse(Common.GetValueInAppSetting("InstructionId", "2"));
                MainPlaceHolder.Controls.Add(control21);
                return;
            }

            if (page == PageConst.Maintain)
            {
                var control21 = (HtmlContent)LoadControl("Usercontrol/HtmlContent.ascx");
                control21.HtmlContentId = short.Parse(Common.GetValueInAppSetting("MaintainId", "2"));
                MainPlaceHolder.Controls.Add(control21);
                return;
            }

            if (page == PageConst.Article)
            {
                var control21 = (HtmlContent)LoadControl("Usercontrol/HtmlContent.ascx");
                control21.HtmlContentId = short.Parse(Request["ArticleId"]);
                MainPlaceHolder.Controls.Add(control21);
                return;
            }
                                
            #endregion

            switch (line)
            {
                case 1:
                    {
                        #region Case 1
                        switch (page)
                        {
                            case PageConst.Category:
                                var control = (ProductBelongCategory)LoadControl("Usercontrol/ProductBelongCategory.ascx");
                                control.CategoryId = short.Parse(Request["CategoryId"]);
                                control.PageSize = 8;
                                MainPlaceHolder.Controls.Add(control);
                                break;
                            case PageConst.Product:
                                var control4 = (BookDetail)LoadControl("Usercontrol/BookDetail.ascx");
                                MainPlaceHolder.Controls.Add(control4);

                                var control7 = (SamCategory)LoadControl("Usercontrol/SamCategory.ascx");
                                control7.PageSize = 4;
                                MainPlaceHolder.Controls.Add(control7);
                                break;
                            case PageConst.Publisher:
                                var control5 = (ProductBelongPublisher)LoadControl("Usercontrol/ProductBelongPublisher.ascx");
                                control5.PublisherId = short.Parse(Request["PublisherId"]);
                                control5.PageSize = 20;
                                MainPlaceHolder.Controls.Add(control5);
                                break;
                            case PageConst.Writer:
                                var control6 = (ProductBelongWriter)LoadControl("Usercontrol/ProductBelongWriter.ascx");
                                control6.WriterId = short.Parse(Request["WriterId"]);
                                control6.PageSize = 20;
                                MainPlaceHolder.Controls.Add(control6);
                                break;
                            default:
                                var control1 = (DiscountProduct)LoadControl("Usercontrol/DiscountProduct.ascx");
                                control1.PageSize = 4;
                                MainPlaceHolder.Controls.Add(control1);
                                var control2 = (FavouriteProduct)LoadControl("Usercontrol/FavouriteProduct.ascx");
                                control2.CategoryId = Common.GetLine();
                                MainPlaceHolder.Controls.Add(control2);
                                var listCategory = Common.GetClientService().GetListChildCategoryByParent(Common.GetLine());
                                foreach (var category in listCategory)
                                {
                                    var subCategories = Common.GetClientService().GetListChildCategoryByParent(category.CategoryId);
                                    foreach (var subCategory in subCategories)
                                    {
                                        var listProduct = Common.GetClientService().GetListProductByCategoryId(subCategory.CategoryId);
                                        if (listProduct.Count > 0)
                                        {
                                            var control3 =
                                                (ProductBelongCategory)
                                                LoadControl("Usercontrol/ProductBelongCategory.ascx");
                                            control3.CategoryId = subCategory.CategoryId;
                                            control3.PageSize = 8;
                                            control3.ShowRows = 8;
                                            MainPlaceHolder.Controls.Add(control3);
                                        }
                                    }
                                }
                            break;
                        }
                        #endregion
                    }
                    break;
                case 2:
                    {
                        #region Case 2
                        switch (page)
                        {
                            case PageConst.Category:
                                var control = (ProductBelongCategory)LoadControl("Usercontrol/ProductBelongCategory.ascx");
                                control.CategoryId = short.Parse(Request["CategoryId"]);
                                control.PageSize = 20;
                                MainPlaceHolder.Controls.Add(control);
                                break;
                            case PageConst.Product:
                                var control4 = (ProductDetail)LoadControl("Usercontrol/ProductDetail.ascx");
                                MainPlaceHolder.Controls.Add(control4);

                                var control17 = (SamCategory)LoadControl("Usercontrol/SamCategory.ascx");
                                control17.PageSize = 4;
                                MainPlaceHolder.Controls.Add(control17);
                                break;
                            case PageConst.Publisher:
                                var control5 = (ProductBelongPublisher)LoadControl("Usercontrol/ProductBelongPublisher.ascx");
                                control5.PublisherId = short.Parse(Request["PublisherId"]);
                                control5.PageSize = 20;
                                MainPlaceHolder.Controls.Add(control5);
                                break;
                                break;
                            default:
                                var control1 = (DiscountProduct)LoadControl("Usercontrol/DiscountProduct.ascx");
                                control1.PageSize = 4;
                                MainPlaceHolder.Controls.Add(control1);
                                var control2 = (FavouriteProduct)LoadControl("Usercontrol/FavouriteProduct.ascx");
                                control2.CategoryId = Common.GetLine();
                                MainPlaceHolder.Controls.Add(control2);
                                var listCategory = Common.GetClientService().GetListChildCategoryByParent(Common.GetLine());
                                foreach (var category in listCategory)
                                {
                                    var subCategories = Common.GetClientService().GetListChildCategoryByParent(category.CategoryId);
                                    foreach (var subCategory in subCategories)
                                    {
                                        var listProduct = Common.GetClientService().GetListProductByCategoryId(subCategory.CategoryId);
                                        if (listProduct.Count > 0)
                                        {
                                            var control3 =
                                                (ProductBelongCategory)
                                                LoadControl("Usercontrol/ProductBelongCategory.ascx");
                                            control3.CategoryId = subCategory.CategoryId;
                                            control3.PageSize = 8;
                                            control3.ShowRows = 8;
                                            MainPlaceHolder.Controls.Add(control3);
                                        }
                                    }
                                }
                                break;
                        }
                        #endregion
                    }
                    break;
                case 3:
                    {
                        #region Case 3                        
                        switch (page)
                        {
                            case PageConst.Category:
                                var control = (ProductBelongCategory)LoadControl("Usercontrol/ProductBelongCategory.ascx");
                                control.CategoryId = short.Parse(Request["CategoryId"]);
                                control.PageSize = 20;
                                MainPlaceHolder.Controls.Add(control);
                                break;
                            case PageConst.Product:
                                var control4 = (ProductDetail)LoadControl("Usercontrol/ProductDetail.ascx");
                                MainPlaceHolder.Controls.Add(control4);

                                var control15 = (SamCategory)LoadControl("Usercontrol/SamCategory.ascx");
                                control15.PageSize = 4;
                                MainPlaceHolder.Controls.Add(control15);
                                break;
                            case PageConst.Publisher:
                                var control5 = (ProductBelongPublisher)LoadControl("Usercontrol/ProductBelongPublisher.ascx");
                                control5.PublisherId = short.Parse(Request["PublisherId"]);
                                control5.PageSize = 20;
                                MainPlaceHolder.Controls.Add(control5);
                                break;
                                break;
                            default:
                                var control1 = (DiscountProduct)LoadControl("Usercontrol/DiscountProduct.ascx");
                                control1.PageSize = 4;
                                MainPlaceHolder.Controls.Add(control1);
                                var control2 = (FavouriteProduct)LoadControl("Usercontrol/FavouriteProduct.ascx");
                                control2.CategoryId = Common.GetLine();
                                MainPlaceHolder.Controls.Add(control2);
                                var listCategory = Common.GetClientService().GetListChildCategoryByParent(Common.GetLine());
                                foreach (var category in listCategory)
                                {
                                    var subCategories = Common.GetClientService().GetListChildCategoryByParent(category.CategoryId);
                                    foreach (var subCategory in subCategories)
                                    {
                                        var listProduct = Common.GetClientService().GetListProductByCategoryId(subCategory.CategoryId);
                                        if (listProduct.Count > 0)
                                        {
                                            var control3 =
                                                (ProductBelongCategory)
                                                LoadControl("Usercontrol/ProductBelongCategory.ascx");
                                            control3.CategoryId = subCategory.CategoryId;
                                            control3.PageSize = 8;
                                            control3.ShowRows = 8;
                                            MainPlaceHolder.Controls.Add(control3);
                                        }
                                    }
                                }
                            break;
                        }
                        #endregion
                    }
                    break;
                default:
                    {
                        #region Default                        
                        switch (page)
                        {
                            case PageConst.Product:
                                var control4 = (ProductDetail)LoadControl("Usercontrol/ProductDetail.ascx");
                                MainPlaceHolder.Controls.Add(control4);

                                var control15 = (SamCategory)LoadControl("Usercontrol/SamCategory.ascx");
                                control15.PageSize = 4;
                                MainPlaceHolder.Controls.Add(control15);
                                break;
                            default:
                                var control1 = (DiscountProduct)LoadControl("Usercontrol/DiscountProduct.ascx");
                                control1.PageSize = 4;
                                MainPlaceHolder.Controls.Add(control1);

                                var control2 = (FavouriteProduct)LoadControl("Usercontrol/FavouriteProduct.ascx");
                                control2.CategoryId = Common.GetLine();
                                MainPlaceHolder.Controls.Add(control2);

                                var control18 = (LastestProduct)LoadControl("Usercontrol/LastestProduct.ascx");
                                control18.CategoryId = 1;
                                MainPlaceHolder.Controls.Add(control18);

                                var control19 = (LastestProduct)LoadControl("Usercontrol/LastestProduct.ascx");
                                control19.CategoryId = 2;
                                MainPlaceHolder.Controls.Add(control19);

                                var control20 = (LastestProduct)LoadControl("Usercontrol/LastestProduct.ascx");
                                control20.CategoryId = 3;
                                MainPlaceHolder.Controls.Add(control20);
                              
                                break;
                        }
                        #endregion
                    }
               break;
            }            
        }
    }
}